package com.jxccp.ui.view.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jxccp.im.chat.common.message.JXConversation;
import com.jxccp.ui.R;
import com.jxccp.ui.utils.JXCommonUtils;
import com.jxccp.ui.utils.JXSmileUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xy on 2017/10/19.
 */

public class JXConversationAdatpter extends JXBasicAdapter<JXConversation, ListView> {

    private LayoutInflater layoutInflater;
    private Context context;

    public JXConversationAdatpter(Context context, List<JXConversation> list) {
        super(context, list);
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Viewholder viewholder = null;
        if (convertView == null) {
            viewholder = new Viewholder();
            convertView = layoutInflater.inflate(R.layout.jx_conversation_item, parent, false);
            viewholder.headImageView = (ImageView) convertView.findViewById(R.id.iv_headimg);
            viewholder.titleView = (TextView) convertView.findViewById(R.id.tv_title);
            viewholder.lastMsgView = (TextView) convertView.findViewById(R.id.tv_lastmessage);
            viewholder.timeStampView = (TextView) convertView.findViewById(R.id.tv_timestamp);
            viewholder.unReadCountTextView=(TextView)convertView.findViewById(R.id.tv_unreadcount);
            viewholder.unReadImageView=(ImageView)convertView.findViewById(R.id.iv_unreadmsg);
            convertView.setTag(viewholder);
        } else {
            viewholder = (Viewholder) convertView.getTag();
        }
        JXConversation conversation = list.get(position);

        //未读消息
        int unreadCount = conversation.getUnreadCount();
        //先把未读消息隐藏
        viewholder.unReadImageView.setVisibility(View.INVISIBLE);
        viewholder.unReadCountTextView.setVisibility(View.INVISIBLE);
        if(unreadCount>0){
            viewholder.unReadImageView.setVisibility(View.VISIBLE);
            viewholder.unReadCountTextView.setVisibility(View.VISIBLE);
            viewholder.unReadCountTextView.setText(unreadCount+"");
        }

        //头像
        if (TextUtils.isEmpty(conversation.getLogoUrl())) {
            viewholder.headImageView.setImageResource(R.drawable.jx_ic_chat_default_contact);
        } else {
            Glide.with(context)
                    .load(conversation.getLogoUrl())
                    .apply(new RequestOptions().placeholder(R.drawable.jx_ic_chat_default_contact))
                    .into(viewholder.headImageView);
        }
        //标题
        viewholder.titleView.setText(conversation.getSubject());
        //消息内容
        Spannable span = null;
        if (conversation.getMessageType() != null) {
            switch (conversation.getMessageType()) {
                case TEXT:
                    String body = conversation.getBody();
                    body = delHTMLTag(body);
                    span = JXSmileUtils.getSmiledText(context, body);
                    viewholder.lastMsgView.setText(span, TextView.BufferType.SPANNABLE);
                    break;
                case IMAGE:
                    viewholder.lastMsgView.setText(context.getResources().getString(
                            R.string.jx_image_message));
                    break;
                case VOICE:
                    viewholder.lastMsgView.setText(context.getResources().getString(
                            R.string.jx_voice_message));
                    break;
                case VIDEO:
                    viewholder.lastMsgView.setText(context.getResources().getString(
                            R.string.jx_video_message));
                    break;
                case NOTIFICATION:
                    viewholder.lastMsgView.setText(conversation.getBody());
                    break;
                case FILE:
                    viewholder.lastMsgView.setText(context.getString(
                            R.string.jx_file_message));
                    break;
                case LOCATION:
                    viewholder.lastMsgView.setText(context.getString(
                            R.string.jx_location_message));
                    break;
                case RICHTEXT:
                    viewholder.lastMsgView.setText(context.getResources().getString(R.string.jx_rich_message));
                    break;
                default:
                    viewholder.lastMsgView.setText("");
                    break;
            }
        } else {
            viewholder.lastMsgView.setText("");
        }
        //时间戳
        String time = JXCommonUtils.readableTimeDifferenceFull(context, conversation.getDate());
        viewholder.timeStampView.setText(time);
        return convertView;
    }



    static class Viewholder {
        ImageView headImageView;
        TextView titleView;
        TextView lastMsgView;
        TextView timeStampView;
        ImageView unReadImageView;
        TextView unReadCountTextView;
    }

    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
    private static final String regEx_space = "\\s*|\t|\r|\n";// 定义空格回车换行符
    private static final String regEx_w = "<w[^>]*?>[\\s\\S]*?<\\/w[^>]*?>";//定义所有w标签

    public static String delHTMLTag(String htmlStr) {
        Pattern p_w = Pattern.compile(regEx_w, Pattern.CASE_INSENSITIVE);
        Matcher m_w = p_w.matcher(htmlStr);
        htmlStr = m_w.replaceAll(""); // 过滤script标签


        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签


        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签


        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签


        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签


        htmlStr = htmlStr.replaceAll(" ", ""); //过滤
        return htmlStr.trim(); // 返回文本字符串
    }

}
